using System;
using ch.ethz.ssh2.channel;
using System.Threading;
namespace ch.ethz.ssh2
{
	
	/// <summary> A <code>LocalStreamForwarder</code> forwards an Input- and Outputstream
	/// pair via the secure tunnel to another host (which may or may not be identical
	/// to the remote SSH-2 server).
	/// 
	/// </summary>
	/// <author>  Christian Plattner
	/// </author>
	/// <version>  2.50, 03/15/10
	/// </version>
	public class LocalStreamForwarder
	{
		/// <returns> An <code>InputStream</code> object.
		/// </returns>
		/// <throws>  IOException </throws>
		virtual public System.IO.Stream InputStream
		{
			get
			{
				return cn.StdoutStream;
			}
			
		}
		/// <summary> Get the OutputStream. Please be aware that the implementation MAY use an
		/// internal buffer. To make sure that the buffered data is sent over the
		/// tunnel, you have to call the <code>flush</code> method of the
		/// <code>OutputStream</code>. To signal EOF, please use the
		/// <code>close</code> method of the <code>OutputStream</code>.
		/// 
		/// </summary>
		/// <returns> An <code>OutputStream</code> object.
		/// </returns>
		/// <throws>  IOException </throws>
		virtual public System.IO.Stream OutputStream
		{
			get
			{
				return cn.StdinStream;
			}
			
		}
		internal ChannelManager cm;
		
		internal System.String host_to_connect;
		internal int port_to_connect;
		internal Thread lat;
		
		internal Channel cn;
		
		internal LocalStreamForwarder(ChannelManager cm, System.String host_to_connect, int port_to_connect)
		{
			this.cm = cm;
			this.host_to_connect = host_to_connect;
			this.port_to_connect = port_to_connect;
			
			cn = cm.openDirectTCPIPChannel(host_to_connect, port_to_connect, "127.0.0.1", 0);
		}
		
		/// <summary> Close the underlying SSH forwarding channel and free up resources.
		/// You can also use this method to force the shutdown of the underlying
		/// forwarding channel. Pending output (OutputStream not flushed) will NOT
		/// be sent. Pending input (InputStream) can still be read. If the shutdown
		/// operation is already in progress (initiated from either side), then this
		/// call is a no-op.
		/// 
		/// </summary>
		/// <throws>  IOException </throws>
		public virtual void  close()
		{
			cm.closeChannel(cn, "Closed due to user request.", true);
		}
	}
}